// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.unit.counter;

import com.dustedpixels.jasmin.unit.Unit;

/**
 * Positive edge triggered counter register with clear and explicit increment.
 * 
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class Counter implements Unit {
  // (input) input clock
  public boolean CLK = false;
  
  // (input) reset
  public boolean RESET;
  
  // (input) load
  public boolean LOAD;
  
  // (input) increment
  public boolean INC;
  
  // (input) input value
  public int IN = 0;
  
  // (output) counter value
  public int OUT;
  
  private boolean lastClock = false;
 
  public void updateEndPoints() {
    if (lastClock != CLK) {
      if (CLK) {
        if (RESET) {
          OUT = 0;
        } else if (LOAD) {
          OUT = IN;
        } else if (INC){
          OUT++;
        }
      }
      lastClock = CLK;
    }
  }
}
